package com.pay.web.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.pay.request.BizContent;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
/**
 * Created by chenweiyi on 2018/1/31.
 */
public class AlipayUtils {


    private static AlipayClient alipayClient = null;

    /**
     * 获取AlipayClient
     *
     * @return
     */
    public static AlipayClient getAlipayClient() {
        if (alipayClient == null) {
            //获得初始化的AlipayClient
            alipayClient = new DefaultAlipayClient(Constants.ALI_GATEWAY,
                    Constants.ALI_APP_ID,
                    Constants.ALI_APP_PRIVATE_KEY,
                    Constants.FORMAT,
                    Constants.CHARSET,
                    Constants.ALI_APP_PUBLIC_KEY,
                    Constants.ALI_SIGN_TYPE);
        }
        return alipayClient;
    }

    /**
     * 支付宝下单
     * @param outTradeNo
     * @param totalAmount
     * @param subject
     * @param returnUrl
     * @param notifyUrl
     * @return
     * @throws AlipayApiException
     */
    public static String postPayAction(String outTradeNo, Float totalAmount, String subject, String returnUrl, String notifyUrl) throws AlipayApiException {

        AlipayClient client = getAlipayClient();
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        request.setReturnUrl(returnUrl);
        request.setNotifyUrl(notifyUrl);
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        model.setOutTradeNo(outTradeNo);
        model.setSubject(subject == null ? "商品" : subject);
        model.setBody("");
        model.setProductCode("QUICK_WAP_PAY");
        request.setBizModel(model);
        try {
            String form = client.pageExecute(request).getBody();
            return form;
        } catch (AlipayApiException e) {
            throw e;
        }

    }

    /**
     * 验证签名
     * @param parameters
     * @return
     * @throws AlipayApiException
     */
    public static boolean signVerified(Map<String, String> parameters) throws AlipayApiException{

        boolean signVerified = AlipaySignature.rsaCheckV1(parameters,Constants.ALI_APP_PUBLIC_KEY, Constants.CHARSET,Constants.ALI_SIGN_TYPE);

        return signVerified;

    }


    /**
     * 支付宝退款接口
     * @param outTradeNo
     * @param tradeNo
     * @param price
     */
    public static boolean postRefundAction(String outTradeNo,String tradeNo,Float price){

        AlipayClient client = getAlipayClient();
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        Map<String,Object> parameters = new HashMap<String,Object>();
        parameters.put("out_trade_no",outTradeNo);
        parameters.put("trade_no",tradeNo);
        parameters.put("refund_amount",price);
        String bizContent = GsonUtils.toJson(parameters);
        request.setBizContent(bizContent);
        try{
            AlipayTradeRefundResponse response = client.execute(request);
            return  response.isSuccess();
        }catch(Exception e){
            e.printStackTrace();
        }
        return false;
    }

}
